package egoc

import (
	"fmt"
	"strings"
)

// 跨域

const (
	cors_Origin           = "Access-Control-Allow-Origin"
	cors_Methods          = "Access-Control-Allow-Methods"
	cors_Headers          = "Access-Control-Allow-Headers"
	cors_Request          = "Access-Control-Request-Headers"
	cors_Expose           = "Access-Control-Expose-Headers"
	cors_AllowCredentials = "Access-Control-Allow-Credentials"
	cors_MaxAge           = "Access-Control-Max-Age"
)

func (c *Context) CorsAddOrigin(origin ...string) *Context {
	c.Writer.Header().Set(cors_Origin, strings.Join(origin, ","))
	return c
}

func (c *Context) CorsAddMethod(method ...string) *Context {
	c.Writer.Header().Set(cors_Methods, strings.Join(method, ","))
	return c
}

func (c *Context) CorsAddHeader(header ...string) *Context {
	c.Writer.Header().Set(cors_Headers, strings.Join(header, ","))
	return c
}

func (c *Context) CorsAddRequest(header ...string) *Context {
	c.Writer.Header().Set(cors_Request, strings.Join(header, ","))
	return c
}

func (c *Context) CorsAddExpose(header ...string) *Context {
	c.Writer.Header().Set(cors_Expose, strings.Join(header, ","))
	return c
}

func (c *Context) CorsSetAllowCredentials(allow bool) *Context {
	c.Writer.Header().Set(cors_AllowCredentials, fmt.Sprintf("%t", allow))
	return c
}

func (c *Context) CorsSetMaxAge(maxAge int64) *Context {
	c.Writer.Header().Set(cors_MaxAge, fmt.Sprintf("%d", maxAge))
	return c
}
